package biz

import "context"

type ConfigSave struct {
	Id          int    `json:"id" gorm:"type:int; primaryKey; autoIncrement"`
	ConfigName  string `json:"configName" gorm:"type:varchar(128); comment:'名称'"`
	ConfigKey   string `json:"configKey" gorm:"type:varchar(64); comment:'键名'"`
	ConfigValue string `json:"configValue" gorm:"type:varchar(256); comment:'键值'"`
	ConfigType  string `json:"configType" gorm:"type:char(1); comment:'1私用 2公开'"`
}

func (ConfigSave) TableName() string {
	return "sys_config"
}

type ConfigRepo interface {
	// CreateConfig 创建配置信息
	// 参数 model 为 ConfigSave数据模型
	CreateConfig(ctx context.Context, model *ConfigSave) (uint, error)
	DeleteConfig(ctx context.Context, model *ConfigSave) (uint, error)
	ListConfig(ctx context.Context, model *ConfigSave, size int, page int) ([]ConfigSave, error)
}

// CreateConfig 创建配置信息
func (cc *ConfigUseCase) CreateConfig(ctx context.Context, id int, name string, key string, value string, types string) (uint, error) {
	// 将“创建用户”函数的参数转换为“用户数据模型”
	configModel := &ConfigSave{
		Id:          id,
		ConfigName:  name,
		ConfigKey:   key,
		ConfigValue: value,
		ConfigType:  types,
	}
	// 调用ConfigRepo接口实现中的CreateConfig函数去创建配置信息
	return cc.repo.CreateConfig(ctx, configModel)
}

// DeleteConfig 创建配置信息
func (cc *ConfigUseCase) DeleteConfig(ctx context.Context, id int) (uint, error) {
	configModel := &ConfigSave{
		Id: id,
	}
	return cc.repo.DeleteConfig(ctx, configModel)
}

// ListConfig 配置信息列表
func (cc *ConfigUseCase) ListConfig(ctx context.Context, name string, key string, value string, types string, page int, size int) ([]ConfigSave, error) {
	configModel := &ConfigSave{
		ConfigName:  name,
		ConfigKey:   key,
		ConfigValue: value,
		ConfigType:  types,
	}
	return cc.repo.ListConfig(ctx, configModel, size, page)
}
